package com.FundamentalDemos.Random;

import java.util.Random;
import java.util.Scanner;

public class 猜数游戏 {
    public static void main(String[] args) {
        //先产生一个1-100之间的随机数,作为答案
        Random random = new Random();
        int answer = random.nextInt(100) + 1;


        //让用户重复猜测,大则提示大,小则提示小,直到猜中为止
        int tryNum = 0;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请猜一个1-100之间的整数:");
            tryNum = scanner.nextInt();
            if (tryNum - answer > 10) {
                System.out.println("你猜的太大了,再试试!");
            } else if (answer - tryNum > 10) {
                System.out.println("你猜的太小了,再试试!");
            } else if (tryNum - answer <= 10 && tryNum - answer > 0) {
                System.out.println("你猜的还大一点点,再试试!");
            } else if (answer - tryNum <= 10 && answer - tryNum > 0) {
                System.out.println("你猜的还小一点点,再试试!");
            } else {
                System.out.println("恭喜你猜中了!");
                break;
            }
        }
    }
}
